<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>10_员工列表练习</title>
</head>
<body>
    <input type="text" placeholder="请输入员工姓名" id="i1">
    <input type="text" placeholder="请输入员工工资" id="i2">
    <input type="text" placeholder="请输入员工岗位" id="i3">
    <button onclick="addEmp()">添加员工</button>
    <hr>
    <table border="1px">
        <caption>员工表</caption>
        <tr>
            <th>姓名</th>
            <th>工资</th>
            <th>岗位</th>
        </tr>
    </table>
    <script>
        //7.准备一个数组,用来保存员工数据,给后端发请求时使用
        let arr = [];
        function addEmp() {
            //1.获取用户在输入框中输入的数据
            let uName = document.getElementById('i1').value.trim();
            let uSalary = document.getElementById('i2').value.trim();
            let uJob = document.getElementById('i3').value.trim();
            //6.对数据进行校验
            if(!uName || !uSalary || !uJob){
                alert('请输入完整信息!');
                return;
            }
            //2.每多一个员工,就要新增1个tr与3个td
            let tr = document.createElement('tr');
            let nameTd = document.createElement('td');
            let salaryTd = document.createElement('td');
            let jobTd = document.createElement('td');
            //3.将收集到的数据放入对应的单元格中
            nameTd.innerHTML = uName;
            salaryTd.innerHTML = uSalary;
            jobTd.innerHTML = uJob;
            //4.将准备好的3个td放入tr中
            tr.append(nameTd, salaryTd, jobTd);
            //5.获取页面上的表格元素
            let table = document.querySelector('table');
            table.append(tr);
            //9.用完的数据要清空!
            document.getElementById('i1').value = '';
            document.getElementById('i2').value = '';
            document.getElementById('i3').value = '';
            //10.将准备好的数据存入数组中
            arr.push({
                name: uName,
                salary: uSalary,
                job: uJob
            });
            console.log(arr);
        }
    </script>

</body>
</html>